#------------------------------------------------------------------------------
# Import Libraries
#------------------------------------------------------------------------------

rm(list = ls())
library(LalRUtils)
libreq(
  data.table, zoo, tictoc, fixest, PanelMatch, patchwork,
  rio, magrittr, janitor, did, panelView, ggplot2, RPushbullet, ggiplot, 
  tidyverse, data.table, zoo, tictoc, fst, fixest, PanelMatch, patchwork,
  rio, magrittr, janitor, did, panelView, ggiplot, tictoc, binsreg, interflex
)

set.seed(42)
theme_set(lal_plot_theme())

notif = \(x) pbPost("note", x)

#------------------------------------------------------------------------------




#------------------------------------------------------------------------------
# Define Paths
#------------------------------------------------------------------------------

# R studio
setwd( dirname(rstudioapi::getActiveDocumentContext()$path) )
# R default : unccoment if you use default R
# setwd(getSrcDirectory(function(){})[1])

#------------------------------------------------------------------------------




#------------------------------------------------------------------------------
# Load Data
#------------------------------------------------------------------------------

vcf <- fread("vcf_data_complete.csv", sep = ",")
vcf[, "t" ] <- vcf$year - 1995
setnames( vcf, "d", "D")
vcf_data <- copy( vcf )
gfc <- fread("gfc_dta.csv" )


#------------------------------------------------------------------------------




#------------------------------------------------------------------------------
# Table for figure 4: Treatment Effects on Annual Deforestation as a 
#           Function of Ex-Ante Forest Cover Cutoffs
#------------------------------------------------------------------------------

library( purrr )
# Check the existance of vcf_data
# Filter data
if (exists("vcf_data")) {
  vcf = vcf_data[year >= 1995]
  rm(vcf_data)
}

# Generate subsample
# estimate with subsample using fixed effects and clusters
# Get results coeficients and standard errors
fitter = function(cutoff){
  dat = gfc[pref_bin >= cutoff]
  m = feols(def_ha ~ D | village[t] + styear, cluster = ~block, dat)
  tidy(m)[, 2:3]
}


fitter2 = function(cutoff){
  dat = gfc[pref_bin >= cutoff]
  m = feols(def_ha ~ D | village[t] + styear, cluster = ~block, dat)
  return(m)
}

models_gfc <- lapply( 1:10, fitter2 )

# Get results and add columns
tic()
cutoff_res = map_dfr(1:10, fitter) %>% setDT
toc()
cutoff_res$n = 1:10
colnames(cutoff_res)[1:2] = c('beta', 'se')


# VCF Robustness
library(dplyr)
vcf[, pref_bin := ntile( cover_1990, 10 ) ]
vcf[, "t"] <- vcf$year-1995
# Function to subsample data
fitter = function(cut) {
  m = feols(forest_index ~ D | cellid[t] + styear, 
            data = vcf[pref_bin >= cut], 
            cluster = ~blk)
  tidy(m)[, 2:3]
}

fitter2 = function(cut) {
  m = feols(forest_index ~ D | cellid[t] + styear, 
            data = vcf[pref_bin >= cut], 
            cluster = ~blk)
  return(m)
}

# Get results and add columns
models_vcf <- lapply( 1:10, fitter2 )

# VCF results

# GFC
sum_stats_gfc = function( cutoff ){
  
  # Filter data
  dat = gfc[ pref_bin >= cutoff ]
  
  # Generate the ever_treated variable
  dat[ , ever_treated := max( D ), .( village ) ]
  
  # Get control and treatment value for pre Y
  ctrl <- round( dat[ ever_treated == 0 & pesa_exposure == 0, 
                      mean( def_ha ) ], 2 )
  treat <- round( dat[ ever_treated == 1 & pesa_exposure == 0, 
                       mean( def_ha ) ], 2 )
  
  year_min <- min(dat$year)
  year_max <- max(dat$year)
  # return vector of values
  return( c( ctrl, treat, year_min, year_max ) )
}

# Generate sum statistics for GFC
controls_gfc <- c()
treated_gfc <- c()
min_years_gfc <- c()
max_years_gfc <- c()
for ( i in 1:10 ){
  res <- sum_stats_gfc( i )
  controls_gfc[ i ] <- res[ 1 ]
  treated_gfc[ i ] <- res[ 2 ]
  min_years_gfc[ i ] <- res[ 3 ]
  max_years_gfc[ i ] <- res[ 4 ]
}


# VCF
sum_stats_vcf = function( cut ){
  
  # Filter data
  dat = vcf[ pref_bin >= cut ]
  dat[, never_treated := max(D) == 0, cellid]
  
  # Get control and treatment value for pre Y
  ctrl <- dat[ never_treated == 1 & year < first_pesa_exposure, 
               mean( forest_index ) ]
  treat <- dat[ never_treated == 0 & year < first_pesa_exposure, 
                mean( forest_index ) ]
  
  year_min <- min(dat$year)
  year_max <- max(dat$year)
  # return vector of values
  return( c( ctrl, treat, year_min, year_max ) )
}

# Generate sum statistics for vcf
controls_vcf <- c()
treated_vcf <- c()
min_years_vcf <- c()
max_years_vcf <- c()
for ( i in 1:10 ){
  res <- sum_stats_vcf( i )
  controls_vcf[ i ] <- res[ 1 ]
  treated_vcf[ i ] <- res[ 2 ]
  min_years_vcf[ i ] <- res[ 3 ]
  max_years_vcf[ i ] <- res[ 4 ]
}

# All models
mods <- append( models_vcf, models_gfc )
treatmap =c(
  # GFC
  "def_ha"       = "Annual Deforestation in Hectares",
  "village"      = "Village",
  "village[t]"   = "Village + Village TT",
  # VCF
  "forest_index" = "Forest cover index",
  "green_index"  = "Non-forest green index",
  "built_index"  = "Non-forest index",
  "cellid"       = "Pixel",
  "cellid[t]"    = "pixel + pixel TT",
  # both
  "yr"           = "Year",
  "year"         = "Year",
  "styear"       = "State $\\times$ Year",
  "D"            = "PESA $\\times$ Scheduled",
  "block"        = "Block",
  "blk"          = "Block"
)
desc = "Deforestation and Forest cover index"
fn = "figure4_regs"; lab = "tab:figure4_regs";
etable(models_vcf,
       style.tex = style.tex(main = "base", 
                             depvar.title = "", 
                             model.title = "", 
                             var.title = "\\midrule", 
                             slopes.title = "\\midrule \\emph{Time Trends}", 
                             yesNo = c("$\\checkmark$", "")),
       signif.code = NA,
       fixef_sizes = T, 
       fixef_sizes.simplify = F,
       fitstat = c( "n", "r2" ), 
       tex = TRUE,
       dict = treatmap,
       label = lab,
       title = glue::glue("Treatment Effects on Annual Deforestation as a Function of Ex-Ante Forest Cover Cutoffs"),
       extralines=list(
         "\\midrule \\emph{Summary Statistics}" = c( rep( "", 10 ) ),
         "Mean Pre-Y (Non-Sch)"= c( controls_vcf ),
         "Mean Pre-Y (Sch )"   = c( treated_vcf ),
         "Dataset"     = c( rep( "VCF", 10 ) ),
         "Timespan"    = c( rep( "1995-2017", 10 ) )
       ),
       file = "main_figure4_table_vcf.tex", 
       replace = TRUE
)

fitstat_register("n_new", function(x) summary( x )$nobs , 
                 "\\# Observations")
etable(models_vcf,
       style.tex = style.tex(main = "base", 
                             depvar.title = "", 
                             model.title = "", 
                             var.title = "\\midrule", 
                             slopes.title = "\\midrule \\emph{Time Trends}", 
                             yesNo = c("$\\checkmark$", "")),
       signif.code = NA,
       fixef_sizes = T, 
       fixef_sizes.simplify = F,
       fitstat = c( "n_new", "r2" ), 
       tex = TRUE,
       dict = treatmap,
       label = lab,
       title = glue::glue("Treatment Effects on Annual Deforestation as a 
                          Function of Ex-Ante Forest Cover Cutoffs"),
       extralines=list(
         "\\midrule \\emph{Summary Statistics}" = c( rep( "", 10 ) ),
         "Mean Pre-Y (Non-Sch)"= c( controls_gfc ),
         "Mean Pre-Y (Sch )"   = c( treated_gfc ),
         "Dataset"     = c( rep( "GFC", 10 ) ),
         "Timespan"    = c( rep( "2001-2017", 10 ) )
       ),
       file = "main_figure4_table_gfc.tex", 
       replace = TRUE
)

#------------------------------------------------------------------------------
